/*
 * 
 * Libman
 * 
 * Daniel Kaufmann, Martin Lang, Simon Moosbrugger,
 * Stefan Mayer, Stephan Halbeisen
 * 
 */
package application.controller.server;

import java.rmi.server.ServerNotActiveException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import model.Client;
import model.Exemplar;
import model.Loan;
import model.dao.DAOFactory;
import application.exceptions.ClientNotFoundException;
import application.exceptions.DatabaseException;
import application.exceptions.ExemplarAlreadyLend;
import application.exceptions.ExemplarNotFoundException;
import application.exceptions.InsufficientClientRightsException;
import application.exceptions.SessionException;
import application.exceptions.SessionException.InvalidIpException;
import application.exceptions.SessionException.SessionDoesNotExistException;
import application.exceptions.SessionException.SessionExpiredException;

/**
 * The Class UseCaseControllerExemplarLoan.
 */
public class UseCaseControllerExemplarLoan extends AbstractUseCaseController {
	public UseCaseControllerExemplarLoan(){
		super();
		
		if(_allowedRoles == null){
			String[] roles = {"Admin", "Employee", "Library"};
			_allowedRoles = Arrays.asList(roles);
		}
	}
	
	/**
	 * Loan medium.
	 * 
	 * @param exemplarId
	 *            the exemplar id
	 * @param clientId
	 *            the client id
	 * @throws ClientNotFoundException
	 *             the client not found exception
	 * @throws ExemplarNotFoundException
	 *             the exemplar not found exception
	 * @throws ExemplarAlreadyLend
	 *             the exemplar already lend
	 * @throws DatabaseException
	 *             the database exception
	 * @throws ServerNotActiveException 
	 * @throws InsufficientClientRightsException 
	 * @throws InvalidIpException 
	 * @throws SessionDoesNotExistException 
	 * @throws SessionExpiredException 
	 */
	public void loanMedium(String sessionId, String ip, int exemplarId, int clientId)
			throws ClientNotFoundException, ExemplarNotFoundException,
			ExemplarAlreadyLend, DatabaseException, SessionException, InsufficientClientRightsException, ServerNotActiveException {
		checkLogin(sessionId, ip);
		
		Exemplar exemplar = DAOFactory.getInstance().getExemplarDAO()
				.findById(exemplarId, false);
		if (exemplar == null) {
			throw new ExemplarNotFoundException();
		} else {
			List<Loan> openLoans = DAOFactory.getInstance().getLoanDAO()
					.getOpenLoans(exemplarId, 10);
			if (openLoans.size() > 0) {
				throw new ExemplarAlreadyLend();
			}
		}

		Client client = DAOFactory.getInstance().getClientDAO()
				.findById(clientId, false);

		if (client == null) {
			throw new ClientNotFoundException();
		}

		GregorianCalendar fromDate = new GregorianCalendar();
		GregorianCalendar toDate = new GregorianCalendar();
		toDate.add(Calendar.DAY_OF_YEAR, client.getGroup()
				.getLoanDurationInDays());
		Loan loan = new Loan(exemplar, 1, fromDate.getTime(), toDate.getTime());
		loan.setExtension(0);
		loan.setClient(client);
		client.getLoans().add(loan);

		DAOFactory.getInstance().getClientDAO().makePersistent(client);
	}
}